// pages/details/history/history.js
var app = getApp();
var time = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sumBorrowingsList: [],
    totalElements: 0,
    currentPage: 1,
    getBorrowingsURL: app.globalData.bookUrl + '/Borrowing/getBorrowings.do',//借阅记录
  },
  onPullDownRefresh: function () {
    var that = this;
    // 显示顶部刷新图标
    that.setData({
      sumBorrowingsList: []
    });
    wx.showNavigationBarLoading({
      title: '正在刷新',
    })
    // 页数+1
    that.getBorrowings(1);

  },
  onReachBottom: function () {
    var that = this;
    var sumBorrowingsListResult = that.data.sumBorrowingsList;
    if (sumBorrowingsListResult.length == that.data.totalElements) { //最大数值
      wx.showToast({
        title: "已到达底部",
        icon: "success",
        duration: 300
      });
      return false;
    } else {
      // 显示加载图标
      // wx.showLoading({
      //   title: '玩命加载中',
      // })
      // 页数+1
      that.getBorrowings(that.data.currentPage);

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var bookName = options.bookName;
    that.setData({
      bookName: bookName
    });
    that.getBorrowings(that.data.currentPage);
  },
  getBorrowings: function (currentPage) {
    var that = this;
    wx.request({
      url: that.data.getBorrowingsURL,
      data: {
        bookName: that.data.bookName,
        currentPage: currentPage,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res.data.data)
        that.showBorrowingsBooks(res.data.data); //向前台传递数据
        if (res.data.data != null) {
          that.setData({
            currentPage: currentPage + 1,
            totalElements: res.data.data.totalElements
          });
        }
      },
      fail: function (error) {
        // fail
        console.log(error)
      }
    })
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },
  showBorrowingsBooks: function (sumBorrowing) {
    var that = this;
    var sumBorrowings = that.data.sumBorrowingsList;
    if (sumBorrowing != null) {
      for (var idx in sumBorrowing.content) {
        var content = sumBorrowing.content[idx];
        var readersName = content.readersName;
        var headImg = content.headImg;
        var borrowingTime = time.formatTime(content.borrowingTime, 'Y-M-D h:m:s');
        var temp = {
          readersName: readersName,
          headImg: headImg,
          borrowingTime: borrowingTime
        }
        sumBorrowings.push(temp);
      }
      that.setData({
        sumBorrowingsList: sumBorrowings
      });
      wx.hideLoading({
        title: '玩命加载中',
      })
    } else {
      wx.showToast({
        title: "没有更多了",
        icon: "success",
        duration: 1500
      })
    }
    
  },
  
})